package com.git.controller;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.git.domain.AjaxJson;
import com.git.domain.DemoEntity;
import com.git.domain.User;
import com.git.service.DemoService;

@Controller
public class DemoController {
	static final ExecutorService executorServicePool = Executors.newFixedThreadPool(50);
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("login")
	public ResponseEntity<AjaxJson> login(HttpServletRequest httpServletRequest){
		httpServletRequest.getSession().setAttribute("user", new User("wang", 18));
		return ResponseEntity.status(200).body(new AjaxJson(true, "成功",null));
	}
	
	@RequestMapping("getAll")
	public ResponseEntity<AjaxJson> getAll(){
		List<DemoEntity> demoEntities = this.demoService.getAll();
		return ResponseEntity.status(200).body(new AjaxJson(true, "成功", demoEntities));
	}

	@RequestMapping("insert")
	public ResponseEntity<AjaxJson> insert(){
		this.demoService.insert();
		return ResponseEntity.status(200).body(new AjaxJson(true, "成功", null));
	}
	
	
	@RequestMapping("insertAll")
	public ResponseEntity<AjaxJson> insert(Integer count) throws Exception{
		for (int i = 0; i < 10000; i++) {
			executorServicePool.submit(new InsertThread());
		}
		return ResponseEntity.status(200).body(new AjaxJson(true, "成功", null));
	}
	
}
